package com.elvin.platform.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.HistoryData;
import com.elvin.platform.beans.PageVo;
import com.elvin.platform.service.HistoryDataService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 历史数据表 前端控制器
 * </p>
 *
 * @author zhenchl
 * @since 2022-05-12
 */

@Controller
@RestController
@RequestMapping("/historyData")
@Log4j2
@Api("历史数据管理")
public class HistoryDataController {

    @Resource
    private HistoryDataService service;

    @ApiIgnore
    @ApiOperation(value = "获取所有历史数据")
    @PostMapping("/queryAll")
    public Result<Object> queryAll() {
        Result<Object> result = new Result<>();
        try {
            List<HistoryData> selectList = service.selectList(null);
            result.success(selectList);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }
    @ApiIgnore
    @ApiOperation(value = "分页获取所有数据")
    @PostMapping("/selectPageOld")
    public Result<Object> selectPageOld(
            @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false)Integer pageNumber,
            @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false)Integer pageSize
    ) {
        Result<Object> result = new Result<>();
        try {
          int count = service.selectCount(null);
          if (count > 0) {
            List<HistoryData> selectList = new ArrayList<>();
            if (pageNumber != null && pageSize != null) {
              Page<HistoryData> page = new Page<>(pageNumber, pageSize);
              service.selectPage(page, null);
              selectList = page.getRecords();
            } else {
              selectList = service.selectList(null);
            }
            result.success(selectList, count);
          } else {
            result.success(null, count);

          }

        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = "2022-1-12";
        try {
            Date parse = simpleDateFormat.parse(reportDate);
            System.out.println(parse);
        }catch (Exception e) {
            log.debug(e.getMessage());
        }

    }
    @ApiOperation(value = "分页获取所有数据")
    @PostMapping("/selectPage")
    public  Object  selectForPage(
            @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false)Integer pageNumber,
            @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false)Integer pageSize,
            @RequestParam(value = "type", required = false)String type,
            //@RequestParam(value = "report_date", required = false) String reportDate,
            @RequestParam(value = "report_start_date", required = false) String reportStartDate,
            @RequestParam(value = "report_end_date", required = false) String reportEndDate,
            @RequestParam(value = "data_sources", required = false)String dataSources,
            @RequestParam(value = "pro_id", required = false)String proId,
            @RequestParam(value = "dev_id", required = false)String devId,
            @RequestParam(value = "dev_code", required = false)String devCode
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            HistoryData historyData = new HistoryData();
            historyData.setType(type==null?"":type);
            historyData.setDataSources(dataSources==null?"":dataSources);
            /*if (reportDate!=null) {
                historyData.setReportDate(simpleDateFormat.parse(reportDate));
            }
            historyData.setReportDateStr(reportDate);*/
            historyData.setReportStartDateStr(reportStartDate);
            historyData.setReportEndDateStr(reportEndDate);
            historyData.setDevId(devId==null?"":devId);
            historyData.setProId(proId==null?"":proId);
            historyData.setDevCode(devCode==null?"":devCode);
            int total = service.selectPageCount(historyData);
            if(total>0){
                PageVo pageVo = new PageVo();
                if (pageNumber !=null && pageSize !=null){
                    pageVo.setPage(pageNumber);
                    pageVo.setPageSize(pageSize);
                }else{
                    pageVo.setPage(1);
                    pageVo.setPageSize(100000000);
                }
                historyData.setPageVo(pageVo);
                List<HistoryData> list = service.selectForPage(historyData);
                map.put("data", list);
            }else{
                map.put("data", null);
            }
            map.put("status", 0);
            map.put("total", total);
            map.put("message", "查询成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
        return map;
    }

    @ApiOperation(value = "新增历史数据")
    @PostMapping("/insert")
    public Result<Object> insert(@RequestBody HistoryData entity) {
        Result<Object> result = new Result<>();
        try {
            service.insert(entity);
            result.success(entity, "新增成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改历史数据")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody HistoryData entity) {
        Result<Object> result = new Result<>();
        try {
            service.updateById(entity);
            result.success("修改成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除历史数据 ")
    @PostMapping("/delete")
    public Result<Object> delete(@ApiParam(name="id",value="历史数据id",required=true) @RequestParam("id") Long id) {
        Result<Object> result = new Result<>();
        try {
            service.deleteById(id);
            result.success("删除成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据ID获取历史数据")
    @PostMapping("/getById")
    public Result<Object> getById(@ApiParam(name="id",value="历史数据id",required=true) @RequestParam("id") Long id) {
        Result<Object> result = new Result<>();
        try {
            HistoryData entity = new HistoryData();
            entity.setId(id);
            entity = service.selectById(entity);
            result.success(entity);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }
}

